package com.scs.application.modules.base.controller;

import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.entity.Dept;
import com.scs.application.modules.base.entity.ReagentTeam;
import com.scs.application.modules.base.service.DeptService;
import com.scs.application.modules.base.service.ReagentTeamService;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * <p>
 * 试剂组 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/base/reagent_team")
public class ReagentTeamController extends CrudController<ReagentTeamService, ReagentTeam>{

    /**
     * 科室人员的对照关系同步到试剂组管人员
     */
    @RequestMapping(value = "/syncUser", method = RequestMethod.GET)
    public RestResult syncUser() {
        Integer updateCount = service.syncUser();
        return RestResult.ok("数据同步成功", updateCount);
    }

    /**
     * 获取当前用户所在的试剂组
     */
    @RequestMapping(value = "/currentTeam", method = RequestMethod.GET)
    public RestResult getCurrentTeam() {
        return RestResult.ok("查询成功", service.getCurrentTeam());
    }

    @Autowired
    private DeptService deptService;

    /**
     * 根据ID查找数据
     */
    @GetMapping("/find")
    public RestResult find(@RequestParam("id") Serializable id) {
        ReagentTeam entity = service.getById(id);
        if (entity == null) {
            return RestResult.error("数据查询失败<br>可能所操作数据已被删除，请刷新后重新尝试操作");
        }
        if (StringUtils.isNotBlank(entity.getDeptId())) {
            Dept dept = deptService.getById(entity.getDeptId());
            if (dept != null) {
                entity.setDeptName(dept.getName());
            }
        }
        return RestResult.ok("获取数据成功", entity);
    }

}
